#-*- encoding: utf-8 -*-

#encoding=utf-8
#@description：蜘蛛抓取内容。

import Queue
import threading
import urllib2
import time
from BeautifulSoup import BeautifulSoup

hosts = ["http://wubotao.com/"]#要抓取的网页

queue = Queue.Queue()
out_queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, queue, out_queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()
            #proxy_support = urllib2.ProxyHandler({'http':'http://xxx.xxx.xxx.xxxx'})#代理IP
            #opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
            #urllib2.install_opener(opener)

            #grabs urls of hosts and then grabs chunk of webpage
            url = urllib2.urlopen(host)
            chunk = url.read()

            #place chunk into out queue
            self.out_queue.put(chunk)

            #signals to queue job is done
            self.queue.task_done()

class DataThread(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, out_queue):
        threading.Thread.__init__(self)
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            chunk = self.out_queue.get()

            #parse the chunk
            soup = BeautifulSoup(chunk)
            titles = soup.findAll(['h1'])
            for title in titles:
                #print type(title)
                t = BeautifulSoup(str(title)) 
                url = t.h1.a['href']
                text = urllib2.urlopen(url).read()
                content = BeautifulSoup(text).find("div", { "class" : "post-entry" })
                print content
                
            #signals to queue job is done
            self.out_queue.task_done()

start = time.time()
def main():
    
    #spawn a pool of threads, and pass them queue instance
    t = ThreadUrl(queue, out_queue)
    t.setDaemon(True)
    t.start()

    #populate queue with data
    for host in hosts:
        queue.put(host)

    dt = DataThread(out_queue)
    dt.setDaemon(True)
    dt.start()

    #wait on the queue until everything has been processed
    queue.join()
    out_queue.join()

main()
print "Elapsed Time: %s" % (time.time() - start)

